package com.ceshiren.hogwarts.wework;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.touch.WaitOptions;
import io.appium.java_client.touch.offset.PointOption;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.touch.TouchActions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Wait;

import java.time.Duration;
import java.util.HashMap;

public class ContactPage extends BasePage{

    static int retry=0;
    static int retry_max=10;

    private final By menu = By.id("j36");
    private final By AddMember = By.id("en3");
    private final By manual = By.xpath("//*[@text='手动输入添加']");
    private final By nameInput = By.id("bf7");
    private final By mobileInput = By.id("gge");
    private final By save = By.id("am4");
    private final By backButton = By.id("j2e");
    private final By closeButton = By.id("j2z");

    By searchButton=By.id("j3d");
    By searchInput=By.id("hl7");
    By member=By.id("cd7");
    By image=By.id("j2z");
    By person=By.id("i6i");
    By moredetail=By.id("j2z");
    By editmember=By.id("bnc");
    By name=By.id("bf7");
    By department=By.id("cd7");

    By savebutton=By.id("j3_");
    By sexselect=By.id("bg7");
    By famel=By.id("c1k");
    By currentName=By.id("ihg");
    By deletebutton=By.id("f9g");
    By conform=By.id("c1p");
    By result=By.id("cuy");



    public ContactPage(AppiumDriver driver) {
        super(driver);
    }

    public ContactPage add(String name, String mobileNum){

        find(menu).click().find(AddMember).click().find(manual).click();
//        driver.findElement(menu).click();
//        driver.findElement(AddMember).click();
//        driver.findElement(manual).click();
        find(nameInput).sendkeys(name);
        find(mobileInput).sendkeys(mobileNum);

        find(save).click();
        find(backButton).click();
        find(closeButton).click();
        find(By.xpath("//*[@text='消息']")).click();
        return this;
    }
    public Member find(String keyword){
        find(searchButton).click();
        find(searchInput).sendkeys(keyword);
        find(member).click();
        find(image).click();
        find(person).click();
        find(moredetail).click();
        find(editmember).click();
        String membername=driver.findElement(name).getText();
        String departmentname=driver.findElement(department).getText();
        return new Member(membername,null,departmentname);

    }

    public Member directfind(String keyword){
        find(searchButton).click();
        find(searchInput).sendkeys(keyword);
        find(member).click();

        find(moredetail).click();
        find(editmember).click();
        String membername=find(name).getText();
        String departmentname=find(department).getText();
        return new Member(membername,null,departmentname);

    }

    public ContactPage indirectfind(String keyword) {
        find(searchButton).click();
        find(searchInput).sendkeys(keyword);
        find(member).click();

        find(moredetail).click();
        find(editmember).click();
        return this;
    }

    public String editMember(String keyword, String currentname) throws InterruptedException {
        indirectfind(keyword);
        find(name).sendkeys(currentname);
        find(sexselect).click();
        find(famel).click();
        find(savebutton).click();
        Thread.sleep(2000);
        String newName=find(currentName).getText();
        return new Member(newName,null,null).getName();

    }
    public String deleteMember(String keyword) throws InterruptedException {
        indirectfind(keyword);

        int width=driver.manage().window().getSize().width;
        int height=driver.manage().window().getSize().height;
        TouchActions touchActions=new TouchActions(driver);
        WebElement el=driver.findElement(By.id("ks"));
       // touchActions.longPress(el).moveByOffset((int)(width*0.5),(int)(height*0.25)).perform();

        touchActions.clickAndHold(el).moveByOffset(width/2,(-height/4)).release().perform();

        find(deletebutton).click();
        find(conform).click();
        String resultText=find(result).getText();

        return resultText;


    }

    public void back() {

        try{
            wait(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@text='通讯录']")),1);
            retry=0;
        }catch (Exception exception){
            if(retry>retry_max){
                throw exception;
            }
            retry+=1;
            driver.navigate().back();
            back();
        }

    }
}
